package com.andova.toolkit.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.FloatRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

/**
 * Created by Zsago on 2017/7/21.
 *
 * @author Zsago
 */
public abstract class ContentViewDialogFragment extends DialogFragment {
    private Dialog dialog;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (dialog != null) return dialog;
        dialog = new Dialog(getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layoutResource());
        Window window = dialog.getWindow();
        assert window != null;
        window.setGravity(gravity());
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * widthPercent());
        lp.height = (int) (dm.heightPixels * heightPercent());
        window.setAttributes(lp);
        onCreateDialog(dialog);
        return dialog;
    }

    @LayoutRes
    protected abstract int layoutResource();

    protected abstract int gravity();

    @FloatRange(from = 0.0f, to = 1.0f)
    protected abstract float widthPercent();

    @FloatRange(from = 0.0f, to = 1.0f)
    protected abstract float heightPercent();

    protected abstract void onCreateDialog(Dialog dialog);
}
